package com.healthcloud.controller;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.constants.UserConsts;
import com.healthcloud.entity.DocPpApplicationEntity;
import com.healthcloud.entity.HlDoctorEntity;
import com.healthcloud.entity.MsgVo;
import com.healthcloud.service.DocPpApplicationService;
import com.healthcloud.service.HlDoctorService;
import com.healthcloud.utils.DateUtils;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 医生签约表
 * 
 * @author yucl
 * @email yucailong1111@126.com
 * @date 2017-09-04 08:29:56
 */
@ApiIgnore
@Controller
@RequestMapping("docppapplication")
public class DocPpApplicationController {

	@Autowired
	private DocPpApplicationService docPpApplicationService;

    @Autowired
    private HlDoctorService hlDoctorService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("docppapplication:list")
	public String list(@RequestParam Map<String, Object> params){
		return "bus/docppapplication/docppapplicationmgr";
	}
	
	@RequiresPermissions("docppapplication:list")
	@RequestMapping(value = "/searchDocPpApplication")
	@ResponseBody
    public MsgVo  searchDocPpApplication(DocPpApplicationEntity bean,Model model, @ModelAttribute("pageParam")PageParam pageParam){

        //todo
        Long userId = ShiroUtils.getUserId();
        HlDoctorEntity doctorEntity = hlDoctorService.queryByUserId(userId);
        if(doctorEntity != null ){
            bean.setHlDoctorEntity(doctorEntity);
        }

        List<DocPpApplicationEntity> list = docPpApplicationService.queryList(bean, pageParam);
        for(DocPpApplicationEntity applicationEntity : list){
            if(applicationEntity.getAggretime() != null){
                int dif = DateUtils.difDays(new Date(),DateUtils.addYear(applicationEntity.getAggretime()));
                applicationEntity.setRemainingTime(dif);
            }
        }
        int count = docPpApplicationService.queryTotal(bean);
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }
	
	/**
	 * 新增页
	 */
	@RequestMapping("/tosave")
	@RequiresPermissions("docppapplication:save")
    public String toSave(Model model) {
        return "bus/docppapplication/addOrUpdatedocppapplication";
    }
    
	/**
	 * 保存
	 */
    @SysLog("保存DocPpApplication")
    @RequestMapping("/save")
    @ResponseBody
    public ApiRes save(DocPpApplicationEntity entity) {

        HlDoctorEntity doctorEntity =  entity.getHlDoctorEntity();
        String status = entity.getConcern();


        //查看是否已经建立关系
        entity.setHlDoctorEntity(null);
        entity.setState(null);
        entity.setConcern(null);
        List<DocPpApplicationEntity> list = docPpApplicationService.queryList(entity, null);
        if (list != null && !list.isEmpty()) {


            for(DocPpApplicationEntity applicationEntity : list){

                if(doctorEntity.getId().equals(applicationEntity.getHlDoctorEntity().getId())){
                    return new ApiRes(false, null, null, "病人和该医生已经申请签约或者已经签约成功");
                }
            }
        }



        //默认为同意状态
        if(StringUtils.isBlank(status)){
            entity.setConcern(UserConsts.AGGRE_STATUS);
        }else{
            entity.setConcern(status);
        }


        entity.setHlDoctorEntity(doctorEntity);
        docPpApplicationService.save(entity);
        return ApiRes.SUCCESS;
    }
	
	/**
	 * 修改页
	 */	
	@RequestMapping("/toupdate")
    @RequiresPermissions("docppapplication:update")
    public String toUpdate(@Param("id") Long id, Model model) {

        DocPpApplicationEntity entity = docPpApplicationService.queryObject(id);
        model.addAttribute("entity", entity);

        return "bus/docppapplication/addOrUpdatedocppapplication";
    }
	
    /**
     * 修改
     */
    @SysLog("修改DocPpApplication")
    @RequestMapping("/update")
    @ResponseBody
    public ApiRes update(DocPpApplicationEntity entity) {

        HlDoctorEntity doctorEntity =  entity.getHlDoctorEntity();
        String status = entity.getConcern();


        //默认为同意状态
        if(StringUtils.isBlank(status)){
            entity.setConcern(UserConsts.AGGRE_STATUS);
        }


        //查看是否已经建立关系
        if(UserConsts.AGGRE_STATUS.equals(entity.getConcern())) {
            entity.setHlDoctorEntity(null);
            entity.setState(null);
            entity.setConcern(null);
            List<DocPpApplicationEntity> list = docPpApplicationService.queryList(entity, null);
        }

        //默认为同意状态
        if(StringUtils.isBlank(status)){
            entity.setConcern(UserConsts.AGGRE_STATUS);
        }else{
            entity.setConcern(status);
        }


        entity.setHlDoctorEntity(doctorEntity);
        docPpApplicationService.update(entity);
        return ApiRes.SUCCESS;
    }

	
    /**
     * 删除
     */
    @SysLog("删除DocPpApplication")
    @RequestMapping("/delete")
    @RequiresPermissions("docppapplication:delete")
    @ResponseBody
    public ApiRes delete(Long[] ids) {
        docPpApplicationService.deleteBatch(ids);
        return ApiRes.SUCCESS;
    }



}
